/*
 * Copyright (c) 2010-2013 Ruben Chakhmakhchyan. All rights reserved.
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Eclipse Public License
 * Version 1.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://opensource.org/licenses/EPL-1.0 and read it before using this file.
 */

#include "threading.h"


#ifdef _WINDOWS

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#endif

#include <windows.h>

#endif


DWORD WINAPI threadStart( LPVOID lpParam );


void* beginInvoke( AsyncInvoke* asyncInvoke )
{
#ifdef _WINDOWS

    DWORD threadId = 0;
    HANDLE hThread = ::CreateThread( NULL, 0, threadStart, asyncInvoke, 0, &threadId );
    if ( hThread == NULL )
    {
        return 0;
    }
    ::CloseHandle( hThread );

    return hThread;
#endif
}

DWORD WINAPI threadStart( LPVOID lpParam )
{
    PluginAsyncInvoke* asyncInvoke = ( PluginAsyncInvoke* ) lpParam;

    asyncInvoke->function( asyncInvoke->arg );

    if( !::PostMessage( ( HWND ) asyncInvoke->handle, WM_CUSTOM_ASYNCCALLBACK, NULL, ( LPARAM ) asyncInvoke ) )
    {
        return 1;
    }

    //CloseHandle( asyncInvoke->threadHandle );

    return 0;
}
